﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace Rio.Framework.Pages.AppCode.Helpers.Behavior
{
    public class DisableRadioButtonBehavior : Behavior<RadioButton>
    {
        public ContentControl ContainerToDisable
        {
            get { return (ContentControl)GetValue(ContainerToDisableProperty); }
            set { SetValue(ContainerToDisableProperty, value); }
        }
        public static readonly DependencyProperty ContainerToDisableProperty =
            DependencyProperty.Register("ContainerToDisable", typeof(ContentControl), typeof(DisableRadioButtonBehavior), null);

        public ContentControl ContainerToEnable
        {
            get { return (ContentControl)GetValue(ContainerToEnableProperty); }
            set { SetValue(ContainerToEnableProperty, value); }
        }
        public static readonly DependencyProperty ContainerToEnableProperty =
            DependencyProperty.Register("ContainerToEnable", typeof(ContentControl), typeof(DisableRadioButtonBehavior), null);

        public DisableRadioButtonBehavior()
            : base()
        {}

        /// <summary>
        /// Called after the behavior is attached to an AssociatedObject.
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.Checked += AssociatedObject_Checked;
        }

        /// <summary>
        /// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.Checked -= AssociatedObject_Checked;
        }

        /// <summary>
        /// Handles the Checked event of the AssociatedObject control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void AssociatedObject_Checked(object sender, RoutedEventArgs e)
        {
            ContainerToDisable.IsEnabled = AssociatedObject.IsChecked == true ? false : true;
            ContainerToEnable.IsEnabled = !ContainerToDisable.IsEnabled;
        }
    }
}
